<?php
/**
 * 功能: 统计管理
 * User: 文丹
 * Date: 2018/8/9 0009
 * Time: 16:25
 */

namespace app\admin\controller;

use app\base\controller\Power;
use think\Db;

class Statistics extends Power{
    /**
     * 商品点击率展示
     */
    public function goods(){
        //商品
        $goods_type = Db::name('goods_type')->select();
        $arr = [];
        $data = [];
        foreach($goods_type as $k=>$v){
            $arr[] = $v['name'];
            $data[$v['name']] = Db::name('goods')->where('gt_id',$v['id'])->sum('browse');
        }
        //用户统计
        $users = Db::name('user')->count();
        $endDate = date('Y-m-d',time());
        $dayEnd = strtotime(date($endDate." 23:59:59"));
        $user_day = Db::name('user')
            ->whereTime('add_time','>=',time())
            ->whereTime('add_time','<=',$dayEnd)
            ->count();//用户今日数
        //今日活跃数
        $active = Db::name('browse')->whereTime('time','>=',$endDate)->count();
        $active_rate = ($users/$active)*100;



       $this->assign([
           'arr'=>$arr,
           'users'=>$users,
           'user_day'=>$user_day,
           'active'=>$active,
           'data'=>$data
       ]) ;
        return $this->fetch('statistics:goods-charts');
    }

    public function index(){
        $goods_type = Db::name('goods_type')->select();
        $arr = [];
        $data = [];
        foreach($goods_type as $k=>$v){
            $arr[] = $v['name'];
            $data[$v['name']] = Db::name('goods')->where('gt_id',$v['id'])->sum('browse');
        }
        /* echo "<pre>";
         print_r($data);
         die;*/
        $this->assign([
            'arr'=>$arr,
            'data'=>$data
        ]) ;
        return $this->fetch('statistics:index');
    }

}